public class Linkedlist {
    static class linklist{
        public linklist prev;
        public linklist next;
        public int val;
        public linklist(int data){
            this.val = data;
        }
    }
    public linklist head;
    public linklist last;

    public int tail(){
        int count = 0;
        linklist temp = head;
        while(temp!=null){
            count++;
             temp = temp.next;
        }
        return count;
    }

    public void addFirst(int data){
        linklist temp = new linklist(data);
        if(head == null){
            head = last = temp;
        }else {
            temp.next = head;
            head.prev = temp;
            head = temp;
        }
    }

    public void addLast(int data){
        linklist temp = new linklist(data);
        if(head == null){
            head = temp;
        }else {
            last.next = temp;
            temp.prev = last;
            last = temp;
        }
    }

    public void add(int data,int pos){
        if(data == 0){
            addFirst(data);
        }
        if (data == tail()){
            addLast(data);
        }
        linklist temp = new linklist(data);
        linklist curr = head;
        while(pos != 0){
            curr = curr.next;
            pos--;
        }
        temp.next = curr.next;
        temp.prev = curr;
        curr.next.prev = temp;
        curr.next = temp;
    }

    public void display(){
        linklist temp = head;
        while(temp != null){
            System.out.print(temp.val + " ");
            temp = temp.next;
        }
        System.out.println();
    }

    public void remove(int value){
        linklist cur = head;
        while (cur != null) {
            if(cur.val == value) {
                //开始删除
                if(cur == head) {
                    head = head.next;
                    if(head != null) {
                        head.prev = null;
                    }
                }else {
                    cur.prev.next = cur.next;
                    if(cur.next == null) {
                        last = last.prev;
                    }else {
                        cur.next.prev = cur.prev;
                    }
                }
                return;
            }
            cur = cur.next;
        }
    }

    public boolean contains(int key){
        linklist temp = head;
        while(temp != null){
            if(temp.val == key){
                return true;
            }
            temp = temp.next;
        }
        return false;
    }

    public void removeAllKey(int key) {
        linklist temp = head;
        while(temp != null){
            if(temp.val == key){
                remove(temp.val);
            }
            temp = temp.next;
        }
    }

    public void clear(){
        linklist temp = head;
        while(temp != null){
            linklist nexttemp = temp.next;
            temp.prev = null;
            temp.next = null;
            temp = nexttemp;
        }
        head = last = null;
    }
}
